package com.tron.dobest.netty.bytebuf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
 * 描述：堆缓冲区
 *
 * @author TRON
 * @since 2020-06-17
 */
public class JVMByteBuf {
    public static void main(String[] args) {
        ByteBuf byteBuf = Unpooled.copiedBuffer("hello world ！", StandardCharsets.UTF_8);
        if (byteBuf.hasArray()) {
            byte[] array = byteBuf.array();
            System.out.println("arrayOffset:"+byteBuf.arrayOffset());
            System.out.println("readerIndex:"+byteBuf.readerIndex());
            int offSet = byteBuf.arrayOffset() + byteBuf.readerIndex();
            int length = byteBuf.readableBytes();
            byte[] bytes = handleArray(array, offSet, length);
            System.out.println(Arrays.toString(bytes));
        }
    }

    private static byte[] handleArray(byte[] array, int offSet, int length) {
        System.out.println("内容：" + Arrays.toString(array));
        System.out.println("偏移量：" + offSet);
        System.out.println("长度："+length);
        return Arrays.copyOf(array,length);
    }
}
